﻿using Microsoft.Phone.Controls;
using SpaceNeedle.SDK.Windows.Platform.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace SpaceNeedle.x6473.WindowsPhone.Views
{
    public class ViewBase<T> : PhoneApplicationPage where T : ViewModelBase, new()
    {
        public ViewBase()
        {
            this.ViewModel = new T();

            //if (!windows.ApplicationModel.DesignMode.DesignModeEnabled)
            //{
                this.DataContext = this.ViewModel;
            //}

            this.Loaded += ViewBase_Loaded;

            //Window.Current.SizeChanged += OnCurrent_SizeChanged;
        }

        void ViewBase_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.ViewModel.IsShareEnabled)
            {
                //this.RegisterForShare();
            }

            this.OnLoaded(e);
        }

        //private void RegisterForShare()
        //{
        //    try
        //    {
        //        DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
        //        dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.ShareTextHandler);
        //    }
        //    catch { }
        //}

        //private void ShareTextHandler(DataTransferManager sender, DataRequestedEventArgs e)
        //{
        //    DataRequest request = e.Request;

        //    ShareInfo info = this.ViewModel.Share();

        //    if (info != null)
        //    {
        //        request.Data.Properties.Title = info.Title; ;
        //        request.Data.Properties.Description = info.Message;

        //        switch (info.Type)
        //        {
        //            case ShareContentType.Image:
        //                break;
        //            case ShareContentType.Link:
        //                request.Data.SetUri(info.Content as Uri);
        //                break;
        //            case ShareContentType.Object:
        //                break;
        //            case ShareContentType.Text:
        //                request.Data.SetText(info.Content.ToString());
        //                break;
        //            default:
        //                break;
        //        }

        //    }
        //}

        

        //private void OnCurrent_SizeChanged(object sender, WindowSizeChangedEventArgs e)
        //{
        //    this.OnSizeChanged(e.Size);
        //}

        protected virtual void OnLoaded(RoutedEventArgs e)
        {
            //this.OnSizeChanged(new windows.Foundation.Size(Window.Current.Bounds.Width, Window.Current.Bounds.Height));
        }

        //protected virtual void OnSizeChanged(Size size)
        //{
        //    var root = VisualTreeHelper.GetChild(this, 0);

        //    if (root == null)
        //    {
        //        return;
        //    }

        //    var groups = VisualStateManager.GetVisualStateGroups(root as FrameworkElement);

        //    if (groups == null || groups.Count == 0 || groups[0].States == null || groups[0].States.Count == 0)
        //    {
        //        return;
        //    }

        //    var states = groups[0].States.ToList();

        //    foreach (var state in states)
        //    {
        //        var id = AutomationProperties.GetAutomationId(state);

        //        if (string.IsNullOrEmpty(id))
        //        {
        //            continue;
        //        }

        //        MediaQuery mediaQuery = new MediaQuery(id, size.Width, size.Height);

        //        if (mediaQuery.IsValid)
        //        {
        //            VisualStateManager.GoToState(this, state.Name, false);
        //        }
        //    }
        //}

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            this.ViewModel.LoadCommand.Execute(null);
        }

        public T ViewModel { get; set; }
    }
}
